<?php
//imports
require_once("Activity.php");
	require_once("Itemtype.php");
	require_once("Itemstatus.php");
	require_once("User.php");
	require_once("User.php");
	require_once("Report.php");
	require_once("Comment.php");
	
class Item_DAO{
	//Variables
		private $id;
		private $title;
		private $description;
		private $created;
		private $allocated;
		private $archived;
		private $targetDate;
		private $Activity_id;
		private $ItemType_id;
		private $ItemStatus_id;
		private $CreatingUser_id;
		private $AllocatedUser_id;
		private $Report_id;
		

	//Accessors
		
	public function getId(){
		return $this->id;
	}
	
	public function setId($p_id){
		$this->id=$p_id;
	}
		
	public function getTitle(){
		return $this->title;
	}
	
	public function setTitle($p_title){
		$this->title=$p_title;
	}
		
	public function getDescription(){
		return $this->description;
	}
	
	public function setDescription($p_description){
		$this->description=$p_description;
	}
		
	public function getCreated(){
		return $this->created;
	}
	
	public function setCreated($p_created){
		$this->created=$p_created;
	}
		
	public function getAllocated(){
		return $this->allocated;
	}
	
	public function setAllocated($p_allocated){
		$this->allocated=$p_allocated;
	}
		
	public function getArchived(){
		return $this->archived;
	}
	
	public function setArchived($p_archived){
		$this->archived=$p_archived;
	}
		
	public function getTargetDate(){
		return $this->targetDate;
	}
	
	public function setTargetDate($p_targetDate){
		$this->targetDate=$p_targetDate;
	}
		
	public function getActivity_id(){
		return $this->Activity_id;
	}
	
	public function setActivity_id($p_Activity_id){
		$this->Activity_id=$p_Activity_id;
	}
		
	public function getItemType_id(){
		return $this->ItemType_id;
	}
	
	public function setItemType_id($p_ItemType_id){
		$this->ItemType_id=$p_ItemType_id;
	}
		
	public function getItemStatus_id(){
		return $this->ItemStatus_id;
	}
	
	public function setItemStatus_id($p_ItemStatus_id){
		$this->ItemStatus_id=$p_ItemStatus_id;
	}
		
	public function getCreatingUser_id(){
		return $this->CreatingUser_id;
	}
	
	public function setCreatingUser_id($p_CreatingUser_id){
		$this->CreatingUser_id=$p_CreatingUser_id;
	}
		
	public function getAllocatedUser_id(){
		return $this->AllocatedUser_id;
	}
	
	public function setAllocatedUser_id($p_AllocatedUser_id){
		$this->AllocatedUser_id=$p_AllocatedUser_id;
	}
		
	public function getReport_id(){
		return $this->Report_id;
	}
	
	public function setReport_id($p_Report_id){
		$this->Report_id=$p_Report_id;
	}
		
	
	//Accessors for foreignKeys
		public function getActivity_id_Ref(){
		return Activity::getFromPrimaryKey($this->getActivity_id());
	}
		public function getItemType_id_Ref(){
		return Itemtype::getFromPrimaryKey($this->getItemType_id());
	}
		public function getItemStatus_id_Ref(){
		return Itemstatus::getFromPrimaryKey($this->getItemStatus_id());
	}
		public function getCreatingUser_id_Ref(){
		return User::getFromPrimaryKey($this->getCreatingUser_id());
	}
		public function getAllocatedUser_id_Ref(){
		return User::getFromPrimaryKey($this->getAllocatedUser_id());
	}
		public function getReport_id_Ref(){
		return Report::getFromPrimaryKey($this->getReport_id());
	}
		
	
	//Method to get referenced objects
		public function getComments(){
		return Comment::getFromCustomCriteria("WHERE Item_id=:value",array("value" => $this->getId()));
	}
			
	/**
	 * Attempt an INSERT if the primary key is empty, otherwise perform an UPDATE.
	 * @param boolean $p_forceCreation true if save must trigger a create (by-pass the automatic ID generation) 
	 */
	public function save($p_forceCreation=false){
		try {
			$l_dbh = new PDO(__DB_URL, __DB_USER, __DB_PASSWORD);
			
						$l_hasKey=$this->getId();
			if(!isset($l_hasKey) || $p_forceCreation){
				//Attempt an INSERT
				if(!$p_forceCreation){
					$this->setId(strval(uniqid("W-", true)) . "-" . strval(rand(100000,999999)));
				}
								$l_sth = $l_dbh->prepare("INSERT INTO item (id,title,description,created,allocated,archived,targetDate,Activity_id,ItemType_id,ItemStatus_id,CreatingUser_id,AllocatedUser_id,Report_id) VALUES (:id,:title,:description,:created,:allocated,:archived,:targetDate,:Activity_id,:ItemType_id,:ItemStatus_id,:CreatingUser_id,:AllocatedUser_id,:Report_id)");
									$l_sth->bindParam(':id',$this->getId());
									$l_sth->bindParam(':title',$this->getTitle());
									$l_sth->bindParam(':description',$this->getDescription());
									$l_sth->bindParam(':created',$this->getCreated());
									$l_sth->bindParam(':allocated',$this->getAllocated());
									$l_sth->bindParam(':archived',$this->getArchived());
									$l_sth->bindParam(':targetDate',$this->getTargetDate());
									$l_sth->bindParam(':Activity_id',$this->getActivity_id());
									$l_sth->bindParam(':ItemType_id',$this->getItemType_id());
									$l_sth->bindParam(':ItemStatus_id',$this->getItemStatus_id());
									$l_sth->bindParam(':CreatingUser_id',$this->getCreatingUser_id());
									$l_sth->bindParam(':AllocatedUser_id',$this->getAllocatedUser_id());
									$l_sth->bindParam(':Report_id',$this->getReport_id());
								if(!$l_sth->execute()){
					throw new PDOException(implode(" - ",$l_sth->errorInfo()));
				}
			}else{
				//Attempt an UPDATE
								$l_sth = $l_dbh->prepare("UPDATE item SET title=:title, description=:description, created=:created, allocated=:allocated, archived=:archived, targetDate=:targetDate, Activity_id=:Activity_id, ItemType_id=:ItemType_id, ItemStatus_id=:ItemStatus_id, CreatingUser_id=:CreatingUser_id, AllocatedUser_id=:AllocatedUser_id, Report_id=:Report_id WHERE id=:id");
									$l_sth->bindParam(':id',$this->getId());
									$l_sth->bindParam(':title',$this->getTitle());
									$l_sth->bindParam(':description',$this->getDescription());
									$l_sth->bindParam(':created',$this->getCreated());
									$l_sth->bindParam(':allocated',$this->getAllocated());
									$l_sth->bindParam(':archived',$this->getArchived());
									$l_sth->bindParam(':targetDate',$this->getTargetDate());
									$l_sth->bindParam(':Activity_id',$this->getActivity_id());
									$l_sth->bindParam(':ItemType_id',$this->getItemType_id());
									$l_sth->bindParam(':ItemStatus_id',$this->getItemStatus_id());
									$l_sth->bindParam(':CreatingUser_id',$this->getCreatingUser_id());
									$l_sth->bindParam(':AllocatedUser_id',$this->getAllocatedUser_id());
									$l_sth->bindParam(':Report_id',$this->getReport_id());
								if(!$l_sth->execute()){
					throw new PDOException(implode(" - ",$l_sth->errorInfo()));
				}				
			}
			
			$l_dbh = null;
		} catch (PDOException $e) {
			print "Error ! : " . $e->getMessage() . "<br/>";
			die();
		}
	}
	
	/**
	 * Delete the current record from the database
	 */
	public function delete(){
		try {
			$l_dbh = new PDO(__DB_URL, __DB_USER, __DB_PASSWORD);
						$l_sth = $l_dbh->prepare("DELETE FROM item WHERE id=:value");
			$l_sth->bindParam(':value', $this->getId());
			if(!$l_sth->execute()){
				throw new PDOException(implode(" - ",$l_sth->errorInfo()));
			}
			//Set the primaryKey to null so that the object might be saved again (=created again)
			$this->setId(null);
		} catch (PDOException $e) {
			print "Error ! : " . $e->getMessage() . "<br/>";
			die();
		}	
	}
	
	
	/**
	 * Return all the objects from the table
	 */
	public static function getAll(){
		try {
			$l_results=array();
			$l_dbh = new PDO(__DB_URL, __DB_USER, __DB_PASSWORD);
			foreach ($l_dbh->query("SELECT * FROM item") as $l_row) {
				$l_object=new Item();
								$l_object->setId($l_row['id']);
								$l_object->setTitle($l_row['title']);
								$l_object->setDescription($l_row['description']);
								$l_object->setCreated($l_row['created']);
								$l_object->setAllocated($l_row['allocated']);
								$l_object->setArchived($l_row['archived']);
								$l_object->setTargetDate($l_row['targetDate']);
								$l_object->setActivity_id($l_row['Activity_id']);
								$l_object->setItemType_id($l_row['ItemType_id']);
								$l_object->setItemStatus_id($l_row['ItemStatus_id']);
								$l_object->setCreatingUser_id($l_row['CreatingUser_id']);
								$l_object->setAllocatedUser_id($l_row['AllocatedUser_id']);
								$l_object->setReport_id($l_row['Report_id']);
								$l_results[$l_row['id']]=$l_object;
			}
			$l_dbh = null;
			return $l_results;
		} catch (PDOException $e) {
			print "Error ! : " . $e->getMessage() . "<br/>";
			die();
		}
	}
	
	/**
	 * Return the object identified by the specified key
	 * @param unknown_type $p_id the primary key value
	 */
	public static function getFromPrimaryKey($p_id){
		try {
			$l_dbh = new PDO(__DB_URL, __DB_USER, __DB_PASSWORD);
			$l_sth = $l_dbh->prepare("SELECT * FROM item WHERE id=:value");
			$l_sth->bindParam(':value', $p_id);
			if(!$l_sth->execute()){
				throw new PDOException(implode(" - ",$l_sth->errorInfo()));
			}
			
			$l_row=$l_sth->fetch(PDO::FETCH_ASSOC);
			if($l_row){
				$l_object=new Item();
								$l_object->setId($l_row['id']);
								$l_object->setTitle($l_row['title']);
								$l_object->setDescription($l_row['description']);
								$l_object->setCreated($l_row['created']);
								$l_object->setAllocated($l_row['allocated']);
								$l_object->setArchived($l_row['archived']);
								$l_object->setTargetDate($l_row['targetDate']);
								$l_object->setActivity_id($l_row['Activity_id']);
								$l_object->setItemType_id($l_row['ItemType_id']);
								$l_object->setItemStatus_id($l_row['ItemStatus_id']);
								$l_object->setCreatingUser_id($l_row['CreatingUser_id']);
								$l_object->setAllocatedUser_id($l_row['AllocatedUser_id']);
								$l_object->setReport_id($l_row['Report_id']);
							}
			$l_dbh = null;
			return $l_object;
		} catch (PDOException $e) {
			print "Error ! : " . $e->getMessage() . "<br/>";
			die();
		}
	}
	
	/**
	 * Return the objects matching the specified SQL SEARCH CLAUSE<br>
	 * WARNING the search is based upon a preparedStatement.<br>
	 * @param string $p_selectionClause a SQL SEARCH CLAUSE (example: WHERE id=:idparam)
	 * @param array $p_arrayOfParameters an array of associations like (param to bind, bind value)
	 */
	public static function getFromCustomCriteria($p_selectionClause, $p_arrayOfParameters){
		try {
			$l_dbh = new PDO(__DB_URL, __DB_USER, __DB_PASSWORD);
			$l_sth = $l_dbh->prepare("SELECT * FROM item {$p_selectionClause}");
			
			$l_parameters=array_keys($p_arrayOfParameters);
			foreach($l_parameters as $l_parameter){
				$l_sth->bindParam($l_parameter, $p_arrayOfParameters[$l_parameter]);
			}
			
			if(!$l_sth->execute()){
				throw new PDOException(implode(" - ",$l_sth->errorInfo()));
			}
			
			$l_rows=$l_sth->fetchAll(PDO::FETCH_ASSOC);
			$l_results=array();
			
			foreach($l_rows as $l_row) {
				$l_object=new Item();
								$l_object->setId($l_row['id']);
								$l_object->setTitle($l_row['title']);
								$l_object->setDescription($l_row['description']);
								$l_object->setCreated($l_row['created']);
								$l_object->setAllocated($l_row['allocated']);
								$l_object->setArchived($l_row['archived']);
								$l_object->setTargetDate($l_row['targetDate']);
								$l_object->setActivity_id($l_row['Activity_id']);
								$l_object->setItemType_id($l_row['ItemType_id']);
								$l_object->setItemStatus_id($l_row['ItemStatus_id']);
								$l_object->setCreatingUser_id($l_row['CreatingUser_id']);
								$l_object->setAllocatedUser_id($l_row['AllocatedUser_id']);
								$l_object->setReport_id($l_row['Report_id']);
								$l_results[$l_row['id']]=$l_object;
			}
			$l_dbh = null;
			return $l_results;
		} catch (PDOException $e) {
			print "Error ! : " . $e->getMessage() . "<br/>";
			die();
		}
	}
	
	
	/**
	 * Return the number of objects matching the specified SQL SEARCH CLAUSE<br>
	 * WARNING the search is based upon a preparedStatement.<br>
	 * @param string $p_selectionClause a SQL SEARCH CLAUSE (example: WHERE id=:idparam)
	 * @param array $p_arrayOfParameters an array of associations like (param to bind, bind value)
	 */
	public static function getCustomCount($p_selectionClause, $p_arrayOfParameters){
		try {
			$l_dbh = new PDO(__DB_URL, __DB_USER, __DB_PASSWORD);
			$l_sth = $l_dbh->prepare("SELECT COUNT(*) as count FROM item {$p_selectionClause}");
			
			$l_parameters=array_keys($p_arrayOfParameters);
			foreach($l_parameters as $l_parameter){
				$l_sth->bindParam($l_parameter, $p_arrayOfParameters[$l_parameter]);
			}
			
			if(!$l_sth->execute()){
				throw new PDOException(implode(" - ",$l_sth->errorInfo()));
			}
			
			$l_row=$l_sth->fetch(PDO::FETCH_ASSOC);
			$l_dbh = null;
			if($l_row){
				return $l_row["count"];
			}else{
				die();
			}
		} catch (PDOException $e) {
			print "Error ! : " . $e->getMessage() . "<br/>";
			die();
		}
	}
	
	
	/**
	 * Return the number of objects<br>
	 */
	public static function getCount(){
		return Item::getCustomCount("",array()); 
	}
}
?>